package com.yhb.controller;


import com.yhb.channel.ChannelFactory;
import com.yhb.controller.form.SimpleVerifyCodeForm;
import com.yhb.controller.form.TradeForm;
import com.yhb.controller.vo.ArrayWrapperVo;
import com.yhb.controller.vo.SimpleVerifyCodeTokenVo;
import com.yhb.model.BranchBank;
import com.yhb.model.Card;
import com.yhb.model.User;
import com.yhb.service.CardService;
import com.yhb.service.TradeService;
import me.edzh.spring.bootstrap.annotations.CurrentUser;
import me.edzh.spring.bootstrap.annotations.EncryptedRequestBody;
import me.edzh.spring.bootstrap.annotations.NeedAuth;
import me.edzh.spring.bootstrap.annotations.SignedRequestBody;
import me.edzh.spring.bootstrap.model.HashMapWrapper;
import me.edzh.spring.bootstrap.model.Response;
import me.edzh.spring.bootstrap.service.ObjectiveStorageService;
import me.edzh.spring.bootstrap.utils.ResultCode;
import me.edzh.spring.bootstrap.utils.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/card")
public class CardController {


    @Autowired
    private CardService cardService;


    @NeedAuth
    @PostMapping("/bind")
    public Response<SimpleVerifyCodeTokenVo> bindCard(@EncryptedRequestBody Card card, @CurrentUser User user) {
        String uuid = cardService.bindCard(card, user);
        return Response.ok(SimpleVerifyCodeTokenVo.builder().token(uuid).build());
    }


    @PostMapping("/verify")
    @NeedAuth
    public Response verifyCard(@SignedRequestBody SimpleVerifyCodeForm form, @CurrentUser User user) {

        cardService.verifyCard(form.getToken(), form.getVerifyCode(), user);
        return Response.ok();
    }

    @PostMapping("/modifyDays")
    @NeedAuth
    public Response modifyCard(@SignedRequestBody Card card, @CurrentUser User user) {
        cardService.cardWithOwnerCheck(card.getId(), user.getId()); // 检查是否本人卡, 不是报错

        cardService.modifyCard(Card.builder()
                .id(card.getId())
                .payDay(card.getPayDay())
                .billDay(card.getBillDay()).build());
        return Response.ok();
    }

    @GetMapping("")
    @NeedAuth
    public Response<ArrayWrapperVo<Card>> getMyCards(@CurrentUser User user) {
        return Response.ok(new ArrayWrapperVo<>(cardService.getUserCard(user.getId())));
    }

    @GetMapping("/{cardId}")
    @NeedAuth
    public Response<Card> getCard(@CurrentUser User user, @PathVariable("cardId")String cardId) {
        return Response.ok(cardService.maskedCard(cardService.cardWithOwnerCheck(cardId, user.getId())));
    }

    @GetMapping("/{cardId}/delete")
    @NeedAuth
    public Response deleteCard(@CurrentUser User user, @PathVariable("cardId")String cardId) {
        cardService.deleteCard(cardId, user.getId());
        return Response.ok();
    }

    @GetMapping("/{cardId}/setDefault")
    @NeedAuth
    public Response setDefaultCard(@CurrentUser User user, @PathVariable("cardId")String cardId) {
        cardService.setDefaultCard(cardId, user.getId());
        return Response.ok();
    }

    @GetMapping("/{bankName}/subBanks/{province}/{city}")
    public Response<ArrayWrapperVo<BranchBank>> searchSubBank(@PathVariable("bankName") String bankName, @PathVariable("province") String province, @PathVariable("city") String city) {
        return Response.ok(new ArrayWrapperVo<>(cardService.searchSubBank(bankName, province, city)));
    }

    @PostMapping("/changePhoneNumber/getCode")
    @NeedAuth
    public Response<SimpleVerifyCodeTokenVo> getChangeCardPhoneNumberToken(@SignedRequestBody Card card, @CurrentUser User user) {
        return Response.ok(new SimpleVerifyCodeTokenVo(cardService.getChangeCardPhoneNumberSms(card.getId(), user.getId(), card.getPhoneNumber()), null));
    }

    @PostMapping("/changePhoneNumber/verify")
    @NeedAuth
    public Response verifyChangeCardPhoneNumber(@SignedRequestBody SimpleVerifyCodeForm form, @CurrentUser User user) {
        cardService.verifyChangeCardNumber(form.getToken(), form.getVerifyCode(), user.getId());
        return Response.ok();
    }


}
